Lando Norris will head team-mate Oscar Piastri off the line for Sunday’s race with Max Verstappen third and Daniel Ricciardo an impressive ninth.
Ricciardo’s performance comes as Perez’s horrid run continued, the Mexican crashing out of qualifying in the first segment, leaving him well down the order.
Rain between Free Practice 3 and the start of qualifying created a number of damp patches, especially in pit lane, though high ambient temperatures quickly saw that dry.
It made slicks the clear option as the entire field headed out in the opening moments as teams were fearful of more rain arriving.
McLaren warned Oscar Piastri as much as he banked the second fastest time, a 1:17.244s.
A mistake from Logan Sargeant under braking for Turn 1 saw the Williams driver lock the brakes heavily, sliding into the barrier on the outside of the downhill right-hander.
He was able to gather the car back up and return to the pits where the car was checked over ahead of another run.
He sat comfortably clear of the drop zone at the time in seventh with a 1:17.770s.
The American fared better than Sergio Perez, who crashed out of the session to draw the red flag.
He lost the back end of the Red Bull Racing at Turn 7, spinning through the small run-off area where he collided heavily with the barrier.
It destroyed the rear of his car and plucked the left-side wheels from it in a crash that could have significant ramifications for the Mexican.
Under the stoppage, the rain continued to fall, prompting race control to disable DRS.
Nonetheless, slick tyres were still preferred when the session resumed 11 minutes after it had been interrupted.
It made for a nervy end to Qualifying 1 for Ricciardo, who sat 16th at the time of the red flag.
Ricciardo began his final flying lap inside the final minute of the segment, improving to the first split with a personal best.
He maintained that through the middle part of the lap too, going on to record a time of 1:17.050s that left him top of the timesheets.
The Australian’s impressive lap left Perez in the bottom five, though his exit from the session was already guaranteed, together with George Russell.
The Mercedes driver used the best of his soft tyre when the track was damp, leaving him exposed as the track dried – not helped by a lack of fuel preventing another attempt.
Also out were Zhou Guanyu and the Alpine duo of Esteban Ocon and Pierre Gasly – Team Enstone having left them sitting in pit lane ahead of the session resuming, where they bled tyre temperature.
Conditions had improved for the start of Qualifying 2, with Hamilton going half a second faster than he had just minutes earlier.
Verstappen then went eight-tenths faster than the Mercedes, his 1:15.660s well over a second faster than the Red Bull Racing driver had gone in Qualifying 1.
Piastri was not far off the Dutchman, mustering a lap just 0.015s slower on his first run.
Of the 15 remaining runners, only Ricciardo didn’t log an initial time as RB kept the Australian in the garage until the final flurry.
He duly delivered a time good enough for seventh, though was bumped as others behind him improved.
That included by RB team-mate Yuki Tsunoda, who went 0.08s faster to slot in eighth, pushing the Australian to ninth.
He remained there as the flag waved, progressing into the final stages of Qualifying 3 while Nico Hulkenberg, Valtteri Bottas, Alex Albon, Logan Sargeant, and Kevin Magnussen missed out.
It was a slow start to Qualifying 3, the silence broken with Verstappen the first car on track as the threat of rain rose once more.
The Dutchman had two sets of new softs available, giving him an advantage on others given only Carlos Sainz, Piastri, and Hamilton were in a position to do the same.
He was instructed that the lap could well count for pole given the closing weather, the championship leader delivering a 1:15.555s.
Piastri was second best though three-tenths slower while Sainz couldn’t break below a 1:16.
Charles Leclerc in the other Ferrari was faster, with a 1:15.905s which provisionally put him third ahead of Hamilton on a 1:15.919s.
They were all bumped back a spot when Norris completed his lap, a 1:15.227s.
Ricciardo was the slowest of the 10 qualifiers, with a 1:16.587s slower than he’d gone in Qualifying 2.
Out of sequence, McLaren ran Piastri for a second time while most were still in the garage.
It left him a near empty track, which he used to full effect to deliver a 1:15.249s and move into provisional second behind his team-mate.
With just over two minutes remaining, the red flag was thrown when Tsunoda crashed.
It was a sizeable impact, the left-rear having been ripped from the RB after he climbed the kerb exiting Turn 5 – launching the car into the barrier in the process.
Enough time existed for the session to be resumed, prompting a race to pit exit well before a restart time had been confirmed.
Verstappen was not among them, climbing from his car, as did Fernando Alonso.
McLaren sent both Norris and Piastri into the queue, the former at the head of it with the latter instructed to make sure he started his timed lap before the chequered flag – and was warned of the prospect his rivals may try and race him for the chance.
When the session did resume, Ricciardo was the only driver improving, recording personal bests through the first two splits.
That saw him improve enough to move ahead of Tsunoda in ninth, the field otherwise unchanged with Norris on pole from Piastri.
Verstappen remained third with Sainz fourth from Hamilton, Leclerc, Alonso, Lance Stroll, Ricciardo, and Tsunoda completing the top 10.
The result marked McLaren’s first front row lock out since 2012.
The Hungarian Grand Prix begins at 15:00 local time (23:00 AEST).